package com.calendar.api.controller;

import com.calendar.api.model.ApiResponse;
import com.calendar.api.model.Event;
import com.calendar.api.model.dto.EventDTO;
import com.calendar.api.security.JwtTokenProvider;
import com.calendar.api.service.EventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/events")
@Api(tags = "日程管理")
public class EventController {

    private final EventService eventService;
    private final JwtTokenProvider jwtTokenProvider;

    @Autowired
    public EventController(EventService eventService, JwtTokenProvider jwtTokenProvider) {
        this.eventService = eventService;
        this.jwtTokenProvider = jwtTokenProvider;
    }

    @GetMapping
    @ApiOperation("获取日程列表")
    public ApiResponse<List<Event>> getEvents(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(eventService.getEvents(startDate, endDate, userId));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取单个日程")
    public ApiResponse<Event> getEvent(@PathVariable Long id, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(eventService.getEvent(id, userId));
    }

    @PostMapping
    @ApiOperation("创建日程")
    public ApiResponse<Event> createEvent(@Valid @RequestBody EventDTO eventDTO, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(eventService.createEvent(eventDTO, userId));
    }

    @PutMapping("/{id}")
    @ApiOperation("更新日程")
    public ApiResponse<Event> updateEvent(@PathVariable Long id, @Valid @RequestBody EventDTO eventDTO, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(eventService.updateEvent(id, eventDTO, userId));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除日程")
    public ApiResponse<Void> deleteEvent(@PathVariable Long id, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        eventService.deleteEvent(id, userId);
        return ApiResponse.success();
    }
} 